package com.ruoyi.model.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.model.domain.ConstructData;
import com.ruoyi.model.service.IConstructDataService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.model.domain.ConstructDetail;
import com.ruoyi.model.service.IConstructDetailService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 建模数据明细Controller
 * 
 * @author WeiHanqiang
 * @date 2025-05-22
 */
@RestController
@RequestMapping("/model/constructDetail")
public class ConstructDetailController extends BaseController
{
    @Autowired
    private IConstructDetailService constructDetailService;

    @Autowired
    private IConstructDataService constructDataService;
    /**
     * 查询建模数据明细列表
     */
    @PreAuthorize("@ss.hasPermi('model:constructDetail:list')")
    @GetMapping("/list")
    public TableDataInfo list(ConstructDetail constructDetail)
    {
        startPage();
        List<ConstructDetail> list = constructDetailService.selectConstructDetailList(constructDetail);
        return getDataTable(list);
    }

    /**
     * 导出建模数据明细列表
     */
    @PreAuthorize("@ss.hasPermi('model:constructDetail:export')")
    @Log(title = "建模数据明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ConstructDetail constructDetail)
    {
        List<ConstructDetail> list = constructDetailService.selectConstructDetailList(constructDetail);
        ExcelUtil<ConstructDetail> util = new ExcelUtil<ConstructDetail>(ConstructDetail.class);
        util.exportExcel(response, list, "建模数据明细数据");
    }

    /**
     * 获取建模数据明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('model:constructDetail:query')")
    @GetMapping(value = "/{constructDetailId}")
    public AjaxResult getInfo(@PathVariable("constructDetailId") Long constructDetailId)
    {
        return success(constructDetailService.selectConstructDetailByConstructDetailId(constructDetailId));
    }

    /**
     * 新增建模数据明细
     */
    @PreAuthorize("@ss.hasPermi('model:constructDetail:add')")
    @Log(title = "建模数据明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ConstructDetail constructDetail)
    {
        constructDetail.setCreator(getUsername());
        constructDetail.setCreatorId(getUserId());
        return toAjax(constructDetailService.insertConstructDetail(constructDetail));
    }

    /**
     * 修改建模数据明细
     */
    @PreAuthorize("@ss.hasPermi('model:constructDetail:edit')")
    @Log(title = "建模数据明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ConstructDetail constructDetail)
    {
        constructDetail.setModifier(getUsername());
        constructDetail.setModifierId(getUserId());
        return toAjax(constructDetailService.updateConstructDetail(constructDetail));
    }

    /**
     * 提交异常剔除的结果
     */
    @PreAuthorize("@ss.hasPermi('model:constructDetail:submitAbnormalPartitionResult')")
    @Log(title = "提交异常剔除的结果", businessType = BusinessType.UPDATE)
    @PutMapping("/submitAbnormalPartitionResult")
    public AjaxResult submitAbnormalPartitionResult(@RequestBody List<ConstructDetail> detailList) {
        String modifier = getUsername();
        Long modifierId = getUserId();

        for (ConstructDetail detail : detailList) {
            detail.setModifier(modifier);
            detail.setModifierId(modifierId);
            constructDetailService.updateConstructDetail(detail);
        }

        return AjaxResult.success("批量更新成功");
    }




    /**
     * 重置建模数据明细
     */
    @PreAuthorize("@ss.hasPermi('model:constructDetail:reset')")
    @Log(title = "建模数据明细", businessType = BusinessType.UPDATE)
    @PutMapping("/reset")
    public AjaxResult reset(@RequestParam Long constructId)
    {
        ConstructData constructData = new ConstructData();
        constructData.setConstructId(constructId);
        constructData.setAbnormalEliminationCode("");
        constructData.setAbnormalEliminationName("");
        constructData.setTestSize(0);
        constructData.setDataPartitioningCode("");
        constructData.setDataPartitioningName("");
        constructData.setConstructStateCode("01");
        constructData.setConstructStateName("暂存");
        constructDataService.updateConstructData(constructData);
        return toAjax(constructDetailService.resetConstructTypeAndStateByConstructId(constructId));
    }

    /**
     * 根据建模数据id删除建模数据明细
     */
    @PreAuthorize("@ss.hasPermi('model:constructDetail:remove')")
    @Log(title = "建模数据明细", businessType = BusinessType.UPDATE)
    @PutMapping("/delete")
    public AjaxResult delete(@RequestParam Long constructId)
    {
        return toAjax(constructDetailService.deleteConstructDetailByConstructId(constructId));
    }

    /**
     * 删除建模数据明细
     */
    @PreAuthorize("@ss.hasPermi('model:constructDetail:remove')")
    @Log(title = "建模数据明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{constructDetailIds}")
    public AjaxResult remove(@PathVariable Long[] constructDetailIds)
    {
        return toAjax(constructDetailService.deleteConstructDetailByConstructDetailIds(constructDetailIds));
    }
}
